<?php

    use EasyWeChat\Foundation\Application;

    require_once $_SERVER['DOCUMENT_ROOT'] . '/wap/core.php';

    require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';

    require_wechat();

    $wechat = new WeChat(-1);
    $config = (object)$wechat->config;

    $options = [
        'debug'  => false,

        'app_id'  => $config->appid,         // AppID
        'secret'  => $config->appsecret,     // AppSecret
        'token'   => 'yhg_weixin_token',          // Token
        'aes_key' => 'LUxtl0VqoHRcd3hkePAGrnj2HjxtJmmroXpQUHWFFoT',


        // 'aes_key' => null, // 可选

        'log' => [
            'level' => 'debug',
            'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
        ],

        'guzzle' => [
            'timeout' => 3.0, // 超时时间（秒）
            'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
        ],

        //...
    ];

    $app = new Application($options);

    $server = $app->server;

    $server->setMessageHandler(function ($message) {

        switch ($message->MsgType) {
            case 'event':
                if(class_exists("Distribute")){
                    if(strpos($message->EventKey,"179317") !== false){
                        $parent_id = preg_replace("/^.*?179317/", "", $message->EventKey);
                        if (empty($parent_id)) {
                            return null;
                        }
                        $flag = Model()->table('proxy_pre')->insert([
                            'openid' => $message->FromUserName,
                            'parent_id' => $parent_id
                        ],true);
                        return "感谢您关注本公众号";
                    }

                }
//                return $message->EventKey;
//                if (strpos($message->Content, 'EVENT_DISTRIBUTE_') !== false) {
//                    $msg = str_replace("EVENT_DISTRIBUTE_","",$message->Content);
//                    //直接增加为该用户下线
//                    return $msg;
//                    return '';
//                    return $message->Content;
//                }
                break;


        }
        return null;
//        return $message->Content;

//        return $message->Recongnition ;
//        return  $message->MsgType . $message->EventKey  . "您好！欢迎关注我!";
    });

    $response = $app->server->serve();

    // 将响应输出
    $response->send();

